import pytest
import requests
import json
from unittest.mock import patch, MagicMock
import time
import random

# 配置信息
API_URL = "https://cvm.tencentcloudapi.com/"
REGION = "ap-guangzhou"
SECRET_ID = "your-secret-id"
SECRET_KEY = "your-secret-key"


def generate_signature(params, secret_key):
    """生成签名（模拟实现）"""
    return "mock-signature"


def send_request(action, params):
    """发送API请求（模拟实现）"""
    common_params = {
        "Action": action,
        "Version": "2017-03-12",
        "Region": REGION,
        "SecretId": SECRET_ID,
        "Timestamp": int(time.time()),
        "Nonce": random.randint(1, 1000000),
        "SignatureMethod": "HmacSHA256"
    }
    all_params = {**common_params, **params}
    all_params["Signature"] = generate_signature(all_params, SECRET_KEY)

    response = requests.post(API_URL, data=all_params)
    return response.json()


class TestReturnInstance:
    @patch('requests.post')
    def test_return_success(self, mock_post):
        """测试成功退还实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "RequestId": "1234567890",
                "TaskId": "task-123456"
            }
        }
        mock_post.return_value = mock_response

        params = {"InstanceIds": ["ins-valid-id"]}
        result = send_request("TerminateInstances", params)

        assert "Response" in result
        assert "TaskId" in result["Response"]

    # 错误码测试用例（共52个）
    @patch('requests.post')
    def test_failed_operation_invalid_application_role(self, mock_post):
        """测试不支持的applicationRole实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "FailedOperation.InvalidInstanceApplicationRole",
                    "Message": "不支持操作的applicationRole实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-approle"]})
        assert result["Response"]["Error"]["Code"] == "FailedOperation.InvalidInstanceApplicationRole"

    @patch('requests.post')
    def test_failed_operation_invalid_emr_instance(self, mock_post):
        """测试不支持EMR的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "FailedOperation.InvalidInstanceApplicationRoleEmr",
                    "Message": "请求不支持EMR的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-emr"]})
        assert result["Response"]["Error"]["Code"] == "FailedOperation.InvalidInstanceApplicationRoleEmr"

    @patch('requests.post')
    def test_failed_operation_unreturnable(self, mock_post):
        """测试实例无法退还"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "FailedOperation.Unreturnable",
                    "Message": "实例无法退还"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-unreturnable"]})
        assert result["Response"]["Error"]["Code"] == "FailedOperation.Unreturnable"

    @patch('requests.post')
    def test_internal_error_trade(self, mock_post):
        """测试交易内部错误"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InternalError.TradeUnknownError",
                    "Message": "内部错误"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-valid-id"]})
        assert result["Response"]["Error"]["Code"] == "InternalError.TradeUnknownError"

    @patch('requests.post')
    def test_internal_server_error(self, mock_post):
        """测试内部服务器错误"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InternalServerError",
                    "Message": "操作内部错误"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-valid-id"]})
        assert result["Response"]["Error"]["Code"] == "InternalServerError"

    @patch('requests.post')
    def test_invalid_account_unpaid_order(self, mock_post):
        """测试账户有未支付订单"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InvalidAccount.UnpaidOrder",
                    "Message": "账户有未支付订单"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-valid-id"]})
        assert result["Response"]["Error"]["Code"] == "InvalidAccount.UnpaidOrder"

    @patch('requests.post')
    def test_invalid_instance_not_supported(self, mock_post):
        """测试不支持的实例类型"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InvalidInstance.NotSupported",
                    "Message": "不被支持的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-unsupported"]})
        assert result["Response"]["Error"]["Code"] == "InvalidInstance.NotSupported"

    @patch('requests.post')
    def test_invalid_instance_id_malformed(self, mock_post):
        """测试无效实例ID格式"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InvalidInstanceId.Malformed",
                    "Message": "无效实例ID格式"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["invalid-format"]})
        assert result["Response"]["Error"]["Code"] == "InvalidInstanceId.Malformed"

    @patch('requests.post')
    def test_invalid_instance_id_not_found(self, mock_post):
        """测试实例不存在"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InvalidInstanceId.NotFound",
                    "Message": "实例不存在"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-not-found"]})
        assert result["Response"]["Error"]["Code"] == "InvalidInstanceId.NotFound"

    @patch('requests.post')
    def test_invalid_instance_not_prepaid(self, mock_post):
        """测试不满足包月退还规则的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InvalidInstanceNotSupportedPrepaidInstance",
                    "Message": "该实例不满足包月退还规则"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-not-prepaid"]})
        assert result["Response"]["Error"]["Code"] == "InvalidInstanceNotSupportedPrepaidInstance"

    @patch('requests.post')
    def test_invalid_parameter_value(self, mock_post):
        """测试参数取值错误"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InvalidParameterValue",
                    "Message": "参数取值错误"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        params = {"InstanceIds": ["ins-valid-id"], "InvalidParam": "invalid"}
        result = send_request("TerminateInstances", params)
        assert result["Response"]["Error"]["Code"] == "InvalidParameterValue"

    @patch('requests.post')
    def test_invalid_instance_id_format(self, mock_post):
        """测试实例ID格式不符合要求"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InvalidParameterValue.InstanceIdMalformed",
                    "Message": "实例ID格式错误"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["INS-VALID-ID"]})
        assert result["Response"]["Error"]["Code"] == "InvalidParameterValue.InstanceIdMalformed"

    @patch('requests.post')
    def test_invalid_appid_format(self, mock_post):
        """测试无效的AppID格式"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InvalidParameterValue.InvalidAppIdFormat",
                    "Message": "无效的appid"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        params = {"InstanceIds": ["ins-valid-id"], "AppId": "invalid-appid"}
        result = send_request("TerminateInstances", params)
        assert result["Response"]["Error"]["Code"] == "InvalidParameterValue.InvalidAppIdFormat"

    @patch('requests.post')
    def test_parameter_limit_exceeded(self, mock_post):
        """测试参数值数量超过限制"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "InvalidParameterValue.LimitExceeded",
                    "Message": "参数值数量超过限制"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        params = {"InstanceIds": ["ins-1"] * 101}  # 假设限制为100个
        result = send_request("TerminateInstances", params)
        assert result["Response"]["Error"]["Code"] == "InvalidParameterValue.LimitExceeded"

    @patch('requests.post')
    def test_unsupported_operation(self, mock_post):
        """测试不支持的操作"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation",
                    "Message": "操作不支持"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-unsupported"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation"

    @patch('requests.post')
    def test_limit_exceeded_return_quota(self, mock_post):
        """测试退还配额已达上限"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "LimitExceeded.UserReturnQuota",
                    "Message": "退还失败，退还配额已达上限"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-valid-id"]})
        assert result["Response"]["Error"]["Code"] == "LimitExceeded.UserReturnQuota"

    @patch('requests.post')
    def test_missing_parameter(self, mock_post):
        """测试缺少必要参数"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "MissingParameter",
                    "Message": "缺少参数错误"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {})
        assert result["Response"]["Error"]["Code"] == "MissingParameter"

    @patch('requests.post')
    def test_mutex_operation_task_running(self, mock_post):
        """测试相同任务正在运行"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "MutexOperation.TaskRunning",
                    "Message": "同样的任务正在运行"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-task-running"]})
        assert result["Response"]["Error"]["Code"] == "MutexOperation.TaskRunning"

    @patch('requests.post')
    def test_operation_denied_in_progress(self, mock_post):
        """测试实例正在执行其他操作"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "OperationDenied.InstanceOperationInProgress",
                    "Message": "实例正在执行其他操作，请稍后再试"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-in-progress"]})
        assert result["Response"]["Error"]["Code"] == "OperationDenied.InstanceOperationInProgress"

    @patch('requests.post')
    def test_unauthorized_invalid_token(self, mock_post):
        """测试无效Token"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnauthorizedOperation.InvalidToken",
                    "Message": "请确认Token是否有效"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-valid-id"]})
        assert result["Response"]["Error"]["Code"] == "UnauthorizedOperation.InvalidToken"

    @patch('requests.post')
    def test_unauthorized_mfa_expired(self, mock_post):
        """测试多因子认证失效"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnauthorizedOperation.MFAExpired",
                    "Message": "您无法进行当前操作，请确认多因子认证（MFA）是否失效"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-valid-id"]})
        assert result["Response"]["Error"]["Code"] == "UnauthorizedOperation.MFAExpired"

    @patch('requests.post')
    def test_unauthorized_mfa_not_found(self, mock_post):
        """测试多因子认证不存在"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnauthorizedOperation.MFANotFound",
                    "Message": "没有权限进行此操作，请确认是否存在多因子认证（MFA）"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-valid-id"]})
        assert result["Response"]["Error"]["Code"] == "UnauthorizedOperation.MFANotFound"

    @patch('requests.post')
    def test_unsupported_operation_charge_type(self, mock_post):
        """测试不支持的实例计费模式"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceChargeType",
                    "Message": "请求不支持该实例计费模式"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-charge-type"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceChargeType"

    @patch('requests.post')
    def test_unsupported_mixed_pricing_model(self, mock_post):
        """测试不支持混合付费模式"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceMixedPricingModel",
                    "Message": "不支持混合付费模式"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-mixed-pricing"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceMixedPricingModel"

    @patch('requests.post')
    def test_unsupported_instance_banned(self, mock_post):
        """测试封禁状态的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateBanning",
                    "Message": "该子机处于封禁状态，请联系相关人员处理"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-banned"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateBanning"

    @patch('requests.post')
    def test_unsupported_enter_rescue_mode(self, mock_post):
        """测试进入救援模式的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateEnterRescueMode",
                    "Message": "请求不支持进入救援模式的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-enter-rescue"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateEnterRescueMode"

    @patch('requests.post')
    def test_unsupported_enter_live_migrate(self, mock_post):
        """测试进入服务迁移的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateEnterServiceLiveMigrate",
                    "Message": "不支持状态为ENTER_SERVICE_LIVE_MIGRATE的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-enter-migrate"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateEnterServiceLiveMigrate"

    @patch('requests.post')
    def test_unsupported_exit_rescue_mode(self, mock_post):
        """测试退出救援模式的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateExitRescueMode",
                    "Message": "请求不支持正在退出救援模式的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-exit-rescue"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateExitRescueMode"

    @patch('requests.post')
    def test_unsupported_exit_live_migrate(self, mock_post):
        """测试退出服务迁移的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateExitServiceLiveMigrate",
                    "Message": "不支持状态为EXIT_SERVICE_LIVE_MIGRATE的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-exit-migrate"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateExitServiceLiveMigrate"

    @patch('requests.post')
    def test_unsupported_instance_frozen(self, mock_post):
        """测试已冻结的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateFreezing",
                    "Message": "操作不支持已冻结的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-frozen"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateFreezing"

    @patch('requests.post')
    def test_unsupported_instance_isolating(self, mock_post):
        """测试正在隔离的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateIsolating",
                    "Message": "请求不支持正在隔离状态的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-isolating"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateIsolating"

    @patch('requests.post')
    def test_unsupported_instance_launch_failed(self, mock_post):
        """测试创建失败的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateLaunchFailed",
                    "Message": "不支持操作创建失败的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-launch-failed"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateLaunchFailed"

    @patch('requests.post')
    def test_unsupported_instance_pending(self, mock_post):
        """测试创建未完成的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStatePending",
                    "Message": "请求不支持创建未完成的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-pending"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStatePending"

    @patch('requests.post')
    def test_unsupported_instance_rebooting(self, mock_post):
        """测试正在重启的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateRebooting",
                    "Message": "请求不支持正在重启的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-rebooting"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateRebooting"

    @patch('requests.post')
    def test_unsupported_instance_starting(self, mock_post):
        """测试正在开机的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateStarting",
                    "Message": "实例开机中，不允许该操作"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-starting"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateStarting"

    @patch('requests.post')
    def test_unsupported_instance_stopping(self, mock_post):
        """测试正在关机的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateStopping",
                    "Message": "请求不支持正在关机的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-stopping"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateStopping"

    @patch('requests.post')
    def test_unsupported_instance_terminated(self, mock_post):
        """测试已销毁的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateTerminated",
                    "Message": "不支持已销毁的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-terminated"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateTerminated"

    @patch('requests.post')
    def test_unsupported_instance_terminating(self, mock_post):
        """测试正在销毁的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstanceStateTerminating",
                    "Message": "请求不支持正在销毁的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-terminating"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstanceStateTerminating"

    @patch('requests.post')
    def test_unsupported_instance_protected(self, mock_post):
        """测试已启用销毁保护的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InstancesProtected",
                    "Message": "不支持已启用销毁保护的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-protected"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InstancesProtected"

    @patch('requests.post')
    def test_unsupported_instance_owner(self, mock_post):
        """测试用户无权限操作的实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.InvalidInstancesOwner",
                    "Message": "用户无权限操作当前实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-other-owner"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.InvalidInstancesOwner"

    @patch('requests.post')
    def test_unsupported_redhat_terminate(self, mock_post):
        """测试不支持退还的RedHat实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.RedHatInstanceTerminateUnsupported",
                    "Message": "实例使用商业操作系统，不支持退还"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-redhat"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.RedHatInstanceTerminateUnsupported"

    @patch('requests.post')
    def test_unsupported_redhat_instance(self, mock_post):
        """测试不支持的RedHat实例"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.RedHatInstanceUnsupported",
                    "Message": "请求不支持操作系统为RedHat的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-redhat"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.RedHatInstanceUnsupported"

    @patch('requests.post')
    def test_unsupported_region(self, mock_post):
        """测试不支持的地域"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.Region",
                    "Message": "不支持该地域"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-unsupported-region"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.Region"

    @patch('requests.post')
    def test_unsupported_special_instance(self, mock_post):
        """测试不支持的特殊机型"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.SpecialInstanceType",
                    "Message": "请求不支持特殊机型的实例"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-special-type"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.SpecialInstanceType"

    @patch('requests.post')
    def test_unsupported_user_quota(self, mock_post):
        """测试用户限额操作配额不足"""
        mock_response = MagicMock()
        mock_response.json.return_value = {
            "Response": {
                "Error": {
                    "Code": "UnsupportedOperation.UserLimitOperationExceedQuota",
                    "Message": "用户限额操作的配额不足"
                },
                "RequestId": "1234567890"
            }
        }
        mock_post.return_value = mock_response

        result = send_request("TerminateInstances", {"InstanceIds": ["ins-valid-id"]})
        assert result["Response"]["Error"]["Code"] == "UnsupportedOperation.UserLimitOperationExceedQuota"


# 启动测试函数
def run_tests():
    pytest.main([__file__, "-v", "-s"])


if __name__ == "__main__":
    run_tests()